home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / µSim 1.1 / source / Microprogram_Ed.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-12  |  29.0 KB  |  1,105 lines  |  [TEXT/CWIE]

  1. /*
  2. Copyright © 1993-1997 Fabrizio Oddone
  3. ••• ••• ••• ••• ••• ••• ••• ••• ••• •••
  4. This source code is distributed as freeware:
  5. you may copy, exchange, modify this code.
  6. You may include this code in any kind of application: freeware,
  7. shareware, or commercial, provided that full credits are given.
  8. You may not sell or distribute this code for profit.
  9. */
  10.  
  11. #include    <Limits.h>
  12.  
  13. #include    "UtilsSys7.h"
  14. #include    "SoundHandling.h"
  15. #include    "Independents.h"
  16. #include    "FabWList.h"
  17. #include    "SimResIDs.h"
  18.  
  19. #include    "Globals.h"
  20. #include    "Animation.h"
  21. #include    "ControlStore.h"
  22. #include    "DoMenu.h"
  23. #include    "Main.h"
  24. #include    "Microprogram_Ed.h"
  25. #include    "SimAsm.h"
  26.  
  27. #if defined(FabSystem7orlater)
  28.  
  29.  
  30. enum {
  31. kLEN_MIR = 4
  32. };
  33.  
  34. /* Cursor keys */
  35. enum cursorkeys {
  36. kcursLeft = 0x1c,
  37. kcursRight,
  38. kcursUp,
  39. kcursDown
  40. };
  41.  
  42. enum microedstrs {
  43. kSTR_ALUINPUT = 1,
  44. kSTR_TO
  45. };
  46.  
  47. enum {
  48. kW_STRINGS = 259
  49. };
  50.  
  51. enum busrequests {
  52. kREQ_NO = 1,
  53. kREQ_READ,
  54. kREQ_WRITE
  55. };
  56.  
  57. static ControlHandle    controls[kNUM_CONTROLS];
  58. static ControlHandle    RadioSelected;
  59. static union u_mir editmir;
  60.  
  61. static TEHandle TEs[2];
  62. ListHandle    Lists[2];
  63.  
  64. RectPtr    keyrects[kN_RECTS];
  65. short    theSelection[2] = { 0, 0 };
  66. short    maxLLine[2];
  67. short    keyDownDest;
  68.  
  69. Boolean    arrowDrawn = false;
  70. Boolean    draggedOnComments = false;
  71.  
  72. /* ===============static prototypes=============== */
  73. static ListHandle MakeList(const RectPtr theListRect, WindowPtr theWindow, ListClickLoopUPP, short lines);
  74. static TEHandle MakeTE(const RectPtr Position);
  75. static void ChangeAnim_mar(short);
  76. static void ChangeAnim_mbr(short);
  77. static void ChangeAnim_map(short);
  78. static void ChangeAnim_amux(short);
  79. static void ChangeAnim_alu(short new, short old);
  80. static void ChangeAnim_cond(short new, short old);
  81. static void ChangeAnim_sh(short new, short old);
  82. static void ChangeAnim_busreq(short new, short old);
  83. static void ChangeAnim_cbus(Boolean new, Boolean old);
  84.  
  85. static void SettheInputTo(TEHandle thisHandle);
  86. #define    Frame()    UnFrame()
  87. static void UnFrame(void);
  88. static void DoChar(short which, unsigned char theChar);
  89. static short StartsWith(short which, unsigned char c);
  90. static void HandleClick(TEHandle theTE, short obj);
  91. static void ChangedComment(void);
  92. static void ChangedBranchTo(void);
  93. static void ResetMirAndComment(void);
  94. static void GotDragOnComments(void);
  95.  
  96. /* =============================================== */
  97.  
  98. #pragma segment Init
  99. /* Init_Microprogram_Ed: initializes the Microprogram window */
  100.  
  101. OSErr Init_Microprogram_Ed(void)
  102. {
  103. enum {
  104. kR_RECTS = 128
  105. };
  106.  
  107. FabWindowPtr    thefabw;
  108. Handle    tempSH;
  109. ControlHandle *ControlScan;
  110. RgnHandle    theRgn;
  111. WindowPtr    w;
  112. Rect    tempRect;
  113. Point tempCell;
  114. short i;
  115. OSErr    err = appMemFullErr;
  116.  
  117. tempCell.h = 0;
  118.     {
  119.     register RectPtr    RectScan, *keyRScan;
  120.     register short    numrects;
  121.     short *numrectsPtr;
  122.  
  123.     RectScan = (RectPtr)((numrectsPtr = (short *)*Get1Resource('nrct', kR_RECTS)) + 1);
  124.     for (numrects = *numrectsPtr, keyRScan = keyrects, i = 0; i < numrects; i++)
  125.         *keyRScan++ = RectScan++;
  126.     }
  127. maxLLine[kL_COMMENTS] = 127;
  128. tempSH = Get1Resource(krInstructions, kOPCODES);
  129. maxLLine[kL_INSTR] = *(unsigned short *)*tempSH;
  130. gWPtr_Microprogram_Ed = w = GetNewWindow(kWIND_Microprogram_Ed, nil, (WindowPtr)-1L);
  131. if (w) {
  132.     SetPortWindowPort(w);
  133.     if (gPrefs.remembWind)
  134.         if (IsOnScreenWeak(gPrefs.MProgTopLeft))
  135.             MoveWindow(w, gPrefs.MProgTopLeft.h, gPrefs.MProgTopLeft.v, false);
  136.     TextFont(geneva);
  137.     TextSize(9);
  138.     for( ControlScan = &controls[kNUM_CONTROLS], i = kRES_CNTL+kNUM_CONTROLS-1;
  139.         i >= kRES_CNTL; i-- )
  140.         *--ControlScan = GetNewControl(i, w);
  141.     Lists[kL_COMMENTS] = MakeList(keyrects[kKEY_LIST], w, gCommentClikLoopUPP,
  142.                                     maxLLine[kL_COMMENTS]+1);
  143.     Lists[kL_INSTR] = MakeList(keyrects[kKEY_INSTR], w, gInstrClikLoopUPP,
  144.                                 maxLLine[kL_INSTR]+1);
  145.     TextFont(systemFont);
  146.     TextSize(12);
  147.     TEs[kKEY_COMMENT] = MakeTE(keyrects[kKEY_COMMENT]);
  148.     (void)TEFeatureFlag(teFOutlineHilite, teBitSet, TEs[kKEY_COMMENT]);
  149.     TEs[kKEY_BRTO] = MakeTE(keyrects[kKEY_BRTO]);
  150.     (void)TEFeatureFlag(teFOutlineHilite, teBitSet, TEs[kKEY_BRTO]);
  151.     TextFont(geneva);
  152.     TextSize(9);
  153.         {
  154.         register ROpcodePtr    instrScan;
  155.         register SignedByte    savedState;
  156.     
  157.         savedState = WantThisHandleSafe(tempSH);
  158.         instrScan = (ROpcodePtr)(((short *)(*tempSH))+1);
  159.         for ( i = 0; i <= maxLLine[kL_INSTR]; i++, instrScan++) {
  160.             tempCell.v = i;
  161.             LSetCell(instrScan, 4, tempCell, Lists[kL_INSTR]);
  162.             }
  163.         HSetState(tempSH, savedState);
  164.         }
  165.     ResetMicroprogramWindow();
  166.     UnloadSeg(ResetMicroprogramWindow);
  167.  
  168.     thefabw = AddWindowToList(w);
  169.     SetActivate(thefabw, Activate_Microprogram_Ed);
  170.     SetUpdate(thefabw, Update_Microprogram_Ed);
  171.     SetGoAway(thefabw, CloseMicroProg);
  172.     SetContent(thefabw, Do_Microprogram_Ed);
  173.  
  174.     for (i = kKEY_BRTO; i <= kKEY_INSTR; i++) {
  175.         theRgn = NewRgn();
  176.         tempRect = *keyrects[i];
  177.         if (i > kKEY_COMMENT)
  178.             tempRect.right -= kScrollbarAdjust;
  179.         RectRgn(theRgn, &tempRect);
  180.         InstallRgnHandler(thefabw, theRgn, nil, i <= kKEY_COMMENT ? gIBeamHandle : gPlusHandle,
  181.                     toMenu(kBalloons_MPrg, i + kNUM_CONTROLS + 1), 0, 0);
  182.         }
  183.  
  184.     for (i = kPOPUP_ABUS; i <= kCHECK_ACTMAP; i++) {
  185.         theRgn = NewRgn();
  186.         RectRgn(theRgn, &(*controls[i])->contrlRect);
  187.         InstallRgnHandler(thefabw, theRgn, nil, nil,
  188.                     toMenu(kBalloons_MPrg, i + 1), 0, 0);
  189.         }
  190.  
  191. // no resizable objects for this window
  192. //    ResizeObjects(w);
  193.  
  194.     RecalcGlobalCoords(thefabw);
  195.  
  196.     err = noErr;
  197.     }
  198. return err;
  199. }
  200.  
  201. /*    MakeList: builds a list given a rectangle, the window,
  202.     and the initial number of lines */
  203.  
  204. static ListHandle MakeList(const RectPtr theListRect, WindowPtr theWindow, ListClickLoopUPP lcUPP, short lines)
  205. {
  206. Rect    dataBounds = { 0, 0, 1, 1};
  207. Rect    tempRect;
  208. Point    cSize = { 0, 0 };
  209. ListHandle    theList;
  210.  
  211. tempRect = *theListRect;
  212. tempRect.right -= kScrollbarAdjust;
  213. dataBounds.bottom = lines;
  214. theList = LNew(&tempRect, &dataBounds, cSize, 0, theWindow, true, false, false, true);
  215. (*theList)->selFlags = lOnlyOne;
  216. if (lcUPP)
  217.     (*theList)->lClickLoop = lcUPP;
  218. LSetSelect(true, cSize, theList);
  219. LSetDrawingMode(true, theList);
  220. return theList;
  221. }
  222.  
  223. /*    MakeTE: builds a TextEdit field, given the rectangle,
  224.     in the current window */
  225.  
  226. static TEHandle MakeTE(const RectPtr Position)
  227. {
  228. Rect    tempRect;
  229.  
  230. tempRect = *Position;
  231. FrameRect(&tempRect);
  232. InsetRect(&tempRect, 3, 3);
  233. return(TENew(&tempRect, &tempRect));
  234. }
  235.  
  236.  
  237. #pragma segment Main
  238. /* EraseArrowRect: erases the rectangle containing the arrow */
  239.  
  240. void EraseArrowRect(void)
  241. {
  242. Rect    tempRect;
  243.  
  244. tempRect.right = keyrects[kKEY_LIST]->left - klateralCellTweek + 1;
  245. tempRect.top = PRCT_T(gWPtr_Microprogram_Ed);
  246. tempRect.left = tempRect.right - karrowDistFromList - 1;
  247. tempRect.bottom = PRCT_B(gWPtr_Microprogram_Ed);
  248. EraseRect(&tempRect);
  249. arrowDrawn = false;
  250. }
  251.  
  252. /* RefreshTE: refreshes a textedit field in the window */
  253.  
  254. void RefreshTE(short whichTE)
  255. {
  256. Rect    tempRect = *keyrects[whichTE];
  257. GrafPtr    savePort;
  258.  
  259. GetPort(&savePort);
  260. SetPort(gWPtr_Microprogram_Ed);
  261. InsetRect(&tempRect, 1, 1);
  262. EraseRect(&tempRect);
  263. TEUpdate(keyrects[whichTE], TEs[whichTE]);
  264. SetPort(savePort);
  265. }
  266.  
  267. /*    SetControlsFromMir: sets up the controls to reflect
  268.     the micro instruction register */
  269.  
  270. void SetControlsFromMir(union u_mir oldmir)
  271. {
  272. Str255 tempS;
  273. GrafPtr    savePort;
  274.  
  275. SetLastMenuCommand(kST_STOPPED);
  276. GetPort(&savePort);
  277. SetPort(gWPtr_Microprogram_Ed);
  278. SetControlValue(controls[kPOPUP_ABUS], editmir.bits.a + 1);
  279. SetControlValue(controls[kPOPUP_BBUS], editmir.bits.b + 1);
  280. if (editmir.bits.c != oldmir.bits.c) {
  281.     SetControlValue(controls[kPOPUP_CBUS], (editmir.bits.dsc ? 0 : editmir.bits.c + 2));
  282.     ChangeAnim_cbus(editmir.bits.dsc, oldmir.bits.dsc);
  283.     }
  284. if (editmir.bits.mar != oldmir.bits.mar) {
  285.     SetControlValue(controls[kCHECK_MAR], editmir.bits.mar);
  286.     ChangeAnim_mar(editmir.bits.mar);
  287.     }
  288. if (editmir.bits.mbr != oldmir.bits.mbr) {
  289.     SetControlValue(controls[kCHECK_MBR], editmir.bits.mbr);
  290.     ChangeAnim_mbr(editmir.bits.mbr);
  291.     }
  292. if ((1 + editmir.bits.rd + (editmir.bits.wr << 1)) !=
  293.     (1 + oldmir.bits.rd + (oldmir.bits.wr << 1))) {
  294.     SetControlValue(controls[kPOPUP_BUSREQ], 1 + editmir.bits.rd + (editmir.bits.wr << 1));
  295.     ChangeAnim_busreq(1 + editmir.bits.rd + (editmir.bits.wr << 1),
  296.                         1 + oldmir.bits.rd + (oldmir.bits.wr << 1));
  297.     }
  298. if (editmir.bits.shift != oldmir.bits.shift) {
  299.     SetControlValue(controls[kPOPUP_SHIFTER], editmir.bits.shift + 1);
  300.     ChangeAnim_sh(editmir.bits.shift, oldmir.bits.shift);
  301.     }
  302. if (editmir.bits.cond != oldmir.bits.cond) {
  303.     SetControlValue(controls[kPOPUP_BRANCH], editmir.bits.cond + 1);
  304.     ChangeAnim_cond(editmir.bits.cond, oldmir.bits.cond);
  305.     }
  306. if (editmir.bits.alu != oldmir.bits.alu) {
  307.     SetControlValue(controls[kPOPUP_ALU], editmir.bits.alu + 1);
  308.     ChangeAnim_alu(editmir.bits.alu, oldmir.bits.alu);
  309.     }
  310. if (editmir.bits.amux != oldmir.bits.amux) {
  311.     SetControlValue(controls[kRADIO_ABUS], 1 - editmir.bits.amux);
  312.     SetControlValue(controls[kRADIO_MBR], editmir.bits.amux);
  313.     RadioSelected = controls[kRADIO_ABUS + editmir.bits.amux];
  314.     ChangeAnim_amux(editmir.bits.amux);
  315.     }
  316. if (editmir.bits.map != oldmir.bits.map) {
  317.     SetControlValue(controls[kCHECK_ACTMAP], editmir.bits.map);
  318.     ChangeAnim_map(editmir.bits.map);
  319.     }
  320. //if (editmir.bits.addr != oldmir.bits.addr) {
  321.     MyNumToString(editmir.bits.addr, tempS);
  322.     TESetText(&tempS[1], StrLength(tempS), TEs[kKEY_BRTO]);
  323.     RefreshTE(kKEY_BRTO);
  324. //    }
  325. SetPort(savePort);
  326. }
  327.  
  328.  
  329. void ChangeAnim_mar(short curVal)
  330. {
  331. static const short martoActDeact[] = { kC_MAR1, kC_MAR2, kC_MAR3, kC_MAR4, 0 };
  332. static const short marifnotSubc[] = { kP_MAR, kP_BLTCH2MAR1, kP_BLTCH2MAR2, 0 };
  333.  
  334. if (curVal) {
  335.     ActivateObjs(martoActDeact);
  336.     if (gRstatus < kST_STEPSUBCYC)
  337.         ActivateObjs(marifnotSubc);
  338.     }
  339. else {
  340.     DeactivateObjs(martoActDeact);
  341.     if (gRstatus < kST_STEPSUBCYC)
  342.         DeactivateObjs(marifnotSubc);
  343.     }
  344. }
  345.  
  346.  
  347. void ChangeAnim_mbr(short curVal)
  348. {
  349. static const short mbrtoActDeact[] = { kC_MBR1, kC_MBR2, kC_MBR3, 0 };
  350. static const short mbrifnotSubc[] = { kP_MBR, kP_SH2MBR1, kP_SH2MBR2, 0 };
  351.  
  352. if (curVal) {
  353.     ActivateObjs(mbrtoActDeact);
  354.     if (gRstatus < kST_STEPSUBCYC)
  355.         ActivateObjs(mbrifnotSubc);
  356.     }
  357. else {
  358.     DeactivateObjs(mbrtoActDeact);
  359.     if (gRstatus < kST_STEPSUBCYC)
  360.         DeactivateObjs(mbrifnotSubc);
  361.     }
  362. }
  363.  
  364.  
  365. void ChangeAnim_map(short curVal)
  366. {
  367. static const short maptoActDeact[] = { kP_MAP, kP_MAPREGS, 0 };
  368.  
  369. if (gRstatus < kST_STEPSUBCYC)
  370.     if (curVal)
  371.         ActivateObjs(maptoActDeact);
  372.     else
  373.         DeactivateObjs(maptoActDeact);
  374. }
  375.  
  376.  
  377. void ChangeAnim_amux(short curVal)
  378. {
  379. static const short amuxtoActDeact[] = { kC_AMUX1, kC_AMUX2, kC_AMUX3, 0 };
  380. static const short amuxifnotSubc[] = { kP_AMUX, kP_MBR2AMUX, 0 };    
  381. static const short amuxtoDeact[] = { kP_ALTCH2AMUX, 0 };
  382. static const short amux2ifnotSubc[] = { kP_MBR2AMUX, 0 };
  383. static const short amuxtoAct[] = { kP_ALTCH2AMUX, kP_AMUX, 0 };
  384.  
  385. if (curVal) {
  386.     ActivateObjs(amuxtoActDeact);
  387.     if (gRstatus < kST_STEPSUBCYC) {
  388.         DeactivateObjs(amuxtoDeact);
  389.         ActivateObjs(amuxifnotSubc);
  390.         }
  391.     }
  392. else {
  393.     DeactivateObjs(amuxtoActDeact);
  394.     if (gRstatus < kST_STEPSUBCYC) {
  395.         ActivateObjs(amuxtoAct);
  396.         DeactivateObjs(amux2ifnotSubc);
  397.         }
  398.     }
  399. }
  400.  
  401.  
  402. void ChangeAnim_alu(short curVal, short oldVal)
  403. {
  404. static const short alutoActDeact[] = { kC_ALU1, kC_ALU2, 0 };
  405. static const short aluifnotSubc[] = { kP_ALU, 0 };
  406.  
  407. if (curVal) {
  408.     if (oldVal == 0) {
  409.         ActivateObjs(alutoActDeact);
  410.         if (gRstatus < kST_STEPSUBCYC)
  411.             ActivateObjs(aluifnotSubc);
  412.         }
  413.     }
  414. else {
  415.     DeactivateObjs(alutoActDeact);
  416.     if (gRstatus < kST_STEPSUBCYC)
  417.         DeactivateObjs(aluifnotSubc);
  418.     }
  419. }
  420.  
  421.  
  422. void ChangeAnim_cond(short curVal, short oldVal)
  423. {
  424. static const short condtoActDeact[] = { kP_MSL, kC_COND1, kC_COND2, kC_MSL2MMUX1, kC_MSL2MMUX2, 0 };
  425.  
  426. if (curVal) {
  427.     if (oldVal == 0)
  428.         ActivateObjs(condtoActDeact);
  429.     }
  430. else
  431.     DeactivateObjs(condtoActDeact);
  432. }
  433.  
  434.  
  435. void ChangeAnim_sh(short curVal, short oldVal)
  436. {
  437. static const short shtoActDeact[] = { kC_SHFT1, kC_SHFT2, 0 };
  438. static const short shifnotSubc[] = { kP_SHIFTER, 0 };
  439.  
  440. if (curVal) {
  441.     if (oldVal == 0) {
  442.         ActivateObjs(shtoActDeact);
  443.         if (gRstatus < kST_STEPSUBCYC)
  444.             ActivateObjs(shifnotSubc);
  445.         }
  446.     }
  447. else {
  448.     DeactivateObjs(shtoActDeact);
  449.     if (gRstatus < kST_STEPSUBCYC)
  450.         DeactivateObjs(shifnotSubc);
  451.     }
  452. }
  453.  
  454.  
  455. void ChangeAnim_busreq(short curVal, short oldVal)
  456. {
  457. static const short busreqNOtoActDeact[] = { kP_MAR2MEM, kP_MBRMEM, 0 };
  458. static const short busreqREADtoActDeact[] = { kC_READ1, kC_READ2, kC_READ3, 0 };
  459. static const short busreqWRITEtoActDeact[] = { kC_WRITE1, kC_WRITE2, kC_WRITE3, 0 };
  460.  
  461. switch (oldVal) {
  462.     case kREQ_NO:
  463.         if (gRstatus < kST_STEPSUBCYC)
  464.             ActivateObjs(busreqNOtoActDeact);
  465.         break;
  466.     case kREQ_READ:
  467.         DeactivateObjs(busreqREADtoActDeact);
  468.         break;
  469.     case kREQ_WRITE:
  470.         DeactivateObjs(busreqWRITEtoActDeact);
  471.     }
  472. switch (curVal) {
  473.     case kREQ_NO:
  474.         if (gRstatus < kST_STEPSUBCYC)
  475.             DeactivateObjs(busreqNOtoActDeact);
  476.         break;
  477.     case kREQ_READ:
  478.         ActivateObjs(busreqREADtoActDeact);
  479.         break;
  480.     case kREQ_WRITE:
  481.         ActivateObjs(busreqWRITEtoActDeact);
  482.     }
  483. }
  484.  
  485.  
  486. void ChangeAnim_cbus(Boolean curVal, Boolean oldVal)
  487. {
  488. static const short cbustoActDeact3[] = { kP_SH2REGS1, kP_SH2REGS2, kP_SH2REGS3, 0 };
  489. static const short cbustoActDeact1[] = { kC_DSC, 0 };
  490.  
  491. if (curVal) {
  492.     if (oldVal == false) {
  493.         if (gRstatus < kST_STEPSUBCYC)
  494.             DeactivateObjs(cbustoActDeact3);
  495.         ActivateObjs(cbustoActDeact1);
  496.         }
  497.     }
  498. else if (oldVal) {
  499.     if (gRstatus < kST_STEPSUBCYC)
  500.         ActivateObjs(cbustoActDeact3);
  501.     DeactivateObjs(cbustoActDeact1);
  502.     }
  503. }
  504.  
  505. /* SettheInputTo: the input TextEdit field has changed */
  506.  
  507. static void SettheInputTo(TEHandle thisHandle)
  508. {
  509. if (gTheInput)
  510.     TEDeactivate(gTheInput);
  511. if (gTheInput = thisHandle)
  512.     TEActivate(gTheInput);
  513. }
  514.  
  515. /* UnFrame: handles the standard bordering for active lists */
  516.  
  517. static void UnFrame(void)
  518. {
  519. Rect    selectRect;
  520. RgnHandle    insideRgn, outsideRgn;
  521.  
  522. selectRect = *keyrects[keyDownDest];
  523. insideRgn = NewRgn();
  524. if (insideRgn) {
  525.     outsideRgn = NewRgn();
  526.     if (outsideRgn) {
  527.         RectRgn(insideRgn, &selectRect);
  528.         InsetRect(&selectRect, -4, -4);
  529.         RectRgn(outsideRgn, &selectRect);
  530.         DiffRgn(outsideRgn, insideRgn, outsideRgn);
  531.         InvalRgn(outsideRgn);
  532.         DisposeRgn(outsideRgn);
  533.         }
  534.     DisposeRgn(insideRgn);
  535.     }
  536. }
  537.  
  538. /* HandleClick: the user has clicked in a list or editable text */
  539.  
  540. static void HandleClick(TEHandle theTE, short obj)
  541. {
  542. if (keyDownDest != obj) {
  543.     if (keyDownDest >= kKEY_LIST)
  544.         UnFrame();
  545.     SettheInputTo(theTE);
  546.     if ((keyDownDest = obj) >= kKEY_LIST)
  547.         Frame();
  548.     }
  549. }
  550.  
  551. /*    PrepareOpenMicroprogram: we hilite some objects before
  552.     showing the window */
  553.  
  554. void PrepareOpenMicroprogram(void)
  555. {
  556. static const short toBeMoreClear[] = { kP_AMUX2ALU, kP_ALU2SH, kP_REG2LTCH1, kP_REG2LTCH2,
  557.                     kP_BLTCH2ALU, kP_CST2MIR, kC_ABC1, kC_ABC2, kC_ABC3, kC_ABC4, 0 };
  558.  
  559. if (((WindowPeek)gWPtr_Microprogram_Ed)->visible == false)
  560.     ActivateObjs(toBeMoreClear);
  561. }
  562.  
  563. void Activate_Microprogram_Ed(EventRecord *, WindowPtr , Handle, short, Boolean Do_An_Activate)
  564. {
  565.  
  566. LActivate(Do_An_Activate, Lists[kL_COMMENTS]);
  567. LActivate(Do_An_Activate, Lists[kL_INSTR]);
  568. if (Do_An_Activate) {
  569.     if (keyDownDest < kKEY_LIST)
  570.         SettheInputTo(TEs[keyDownDest]);
  571.     else
  572.         Frame();
  573.     }
  574. else {
  575.     if (keyDownDest >= kKEY_LIST)
  576.         UnFrame();
  577.     else
  578.         SettheInputTo(nil);
  579.     }
  580. }
  581.  
  582. /* Microprog_TextWasModified: tells us that a text field has been modified */
  583.  
  584. void Microprog_TextWasModified(void)
  585. {
  586. if (gTheInput == TEs[kKEY_COMMENT])
  587.     ChangedComment();
  588. else if (gTheInput == TEs[kKEY_BRTO])
  589.     ChangedBranchTo();
  590. DocumentIsDirty(true);
  591. }
  592.  
  593. /* ChangedComment: the comment field has changed */
  594.  
  595. static void ChangedComment(void)
  596. {
  597. Handle    typedText;
  598. Point    tempCell;
  599. SignedByte    savedState;
  600.  
  601. tempCell.h = 0;
  602. tempCell.v = theSelection[kL_COMMENTS];
  603. savedState = WantThisHandleSafe(typedText = (Handle)TEGetText(TEs[kKEY_COMMENT]));
  604. LSetCell(*typedText, InlineGetHandleSize(typedText), tempCell, Lists[kL_COMMENTS]);
  605. HSetState(typedText, savedState);
  606. }
  607.  
  608. /* ChangedBranchTo: the branch to… field has changed */
  609.  
  610. static void ChangedBranchTo(void)
  611. {
  612. Str255    tempS;
  613. Size    tSize = 0L;
  614. register union u_mir    tempMir;
  615.  
  616. GetDialogItemText(TEGetText(TEs[kKEY_BRTO]), tempS);
  617. if (tempS[0] != 0)
  618.     StringToNum(tempS, &tSize);
  619. /* …stuff it into the micro instruction */
  620. tempMir = editmir;
  621. tempMir.bits.addr = tSize;
  622. SetMir(tempMir.cstore);
  623. }
  624.  
  625. /* ChangedListSelection: the selection in a list has changed */
  626.  
  627. void ChangedListSelection(Point newCell, short which, Boolean inCkLoop)
  628. {
  629. Str255    tempS;
  630. GrafPtr    savePort;
  631. register ROpcodePtr    myOpcodePtr;
  632. Point instr;
  633. short    length;
  634.  
  635. GetPort(&savePort);
  636. SetPort(gWPtr_Microprogram_Ed);
  637. if (which == kL_COMMENTS) {
  638.     if (inCkLoop) {
  639. /* we must update the associative memory… */
  640.         *(long *)&instr = 0L;
  641.         if (LGetSelect(true, &instr, Lists[kL_INSTR])) {
  642.             register Byte i;
  643.  
  644.             myOpcodePtr = ((ROpcodePtr)(((short *)*Get1Resource(krInstructions, kOPCODES)) + 1))
  645.                             + instr.v;
  646.             for (i = myOpcodePtr->offsetHB; i <= myOpcodePtr->lastHB; i++)
  647.                 *(gAssMemory + i) = newCell.v;
  648.             }
  649.         }
  650. /* …and update the control store memory */
  651. /*    *(csMemory + theSelection[kL_COMMENTS]) = editmir;*/
  652. /* get cell contents and put into TextEdit field */
  653.     length = 255;    /* maximum length of text */
  654.     LGetCell(&tempS, &length, newCell, Lists[kL_COMMENTS]);
  655.     TESetText(&tempS, length, TEs[kKEY_COMMENT]);
  656.     RefreshTE(kKEY_COMMENT);
  657.     theSelection[which] = newCell.v;
  658. /* load the micro instruction we must edit from control store memory */
  659.     SetMir((*(gCsMemory + newCell.v)).cstore);
  660.     }
  661. else {    /* instruction list */
  662.     myOpcodePtr = ((ROpcodePtr)(((short *)*Get1Resource(krInstructions, kOPCODES))+1))
  663.                     + newCell.v;
  664.     if (*(gAssMemory + myOpcodePtr->offsetHB) != theSelection[kL_COMMENTS])
  665. /* the selected instruction is associated to a "comment" not selected */
  666.         SelectLLine(kL_COMMENTS, *(gAssMemory + myOpcodePtr->offsetHB));
  667.     }
  668. theSelection[which] = newCell.v;
  669. SetPort(savePort);
  670. }
  671.  
  672. void Do_Microprogram_Ed(WindowPtr w, EventRecord *theEvent)
  673. {
  674. Rect    tempRect;
  675. union u_mir    oldmir;
  676. Point    myPt;
  677. ControlHandle    theControl;
  678. Boolean    DoubleClick, selectionFound;
  679.  
  680. myPt = theEvent->where;
  681. GlobalToLocal(&myPt);
  682. /* click in comment TE */
  683. if (PtInRect(myPt, keyrects[kKEY_COMMENT])) {
  684.     HandleClick(TEs[kKEY_COMMENT], kKEY_COMMENT);
  685.     TEClick(myPt, (theEvent->modifiers & shiftKey) != 0, TEs[kKEY_COMMENT]);
  686.     }
  687. /* click in branch to line TE */
  688. else if (PtInRect(myPt, keyrects[kKEY_BRTO])) {
  689.     HandleClick(TEs[kKEY_BRTO], kKEY_BRTO);
  690.     TEClick(myPt, (theEvent->modifiers & shiftKey) != 0, TEs[kKEY_BRTO]);
  691.     }
  692. else {
  693.     tempRect = *keyrects[kKEY_LIST];
  694.     InsetRect(&tempRect, -1, -1);
  695. /* click in comment list */
  696.     if (PtInRect(myPt, &tempRect)) {
  697.         HandleClick(nil, kKEY_LIST);
  698.         if (theEvent->modifiers & cmdKey)
  699.             SetCursor(*GetCursor(plusCursor));
  700.         DoubleClick = LClick(myPt, theEvent->modifiers, Lists[kL_COMMENTS]);
  701.         EraseArrowRect();
  702.         *(long *)(&myPt) = 0L;
  703.         selectionFound = LGetSelect(true, &myPt, Lists[kL_COMMENTS]);
  704.         if (theEvent->modifiers & cmdKey) {
  705.             oldmir = editmir;
  706.             oldmir.bits.addr = myPt.v;
  707.             SetMir(oldmir.cstore);
  708.             DocumentIsDirty(true);
  709.             InitCursor();
  710.             }
  711.         if (DoubleClick) {
  712.             if (editmir.bits.cond)
  713.                 SelectLLine(kL_COMMENTS, editmir.bits.addr);
  714.             }
  715.         else {    // big bug here (doubleclick, move, & release mouse)
  716.             if (selectionFound) {
  717.                 if (myPt.v != theSelection[kL_COMMENTS])
  718.                     /* new item has been selected */
  719.                     ChangedListSelection(myPt, kL_COMMENTS, false);
  720.                 }
  721.             }
  722.         }
  723.     else {
  724.         tempRect = *keyrects[kKEY_INSTR];
  725.         InsetRect(&tempRect, -1, -1);
  726. /* click in instruction list */
  727.         if (PtInRect(myPt, &tempRect)) {
  728.             HandleClick(nil, kKEY_INSTR);
  729.             (void)LClick(myPt, theEvent->modifiers, Lists[kL_INSTR]);
  730.             if (draggedOnComments) {
  731.                 GotDragOnComments();
  732.                 }
  733.             tempRect = *keyrects[kKEY_INSTR];
  734.             tempRect.right -= kScrollbarAdjust;
  735.             if (PtInRect(myPt, &tempRect)) {
  736.                 *(long *)(&myPt) = 0L;
  737.                 if (LGetSelect(true, &myPt, Lists[kL_INSTR]))
  738.                     /* new item has been selected */
  739.                     ChangedListSelection(myPt, kL_INSTR, draggedOnComments);
  740.                 }
  741.             draggedOnComments = false;
  742.             ForceMouseMovedEvent();
  743.             }
  744.         else {
  745. /* click in controls */
  746.             if (FindControl(myPt, w, &theControl)) {
  747.                 oldmir = editmir;
  748.                 switch(TrackControl(theControl, myPt, (ControlActionUPP)-1L)) {
  749.                     register short newval;
  750.         
  751.                     case kControlLabelPart:
  752.                         if (theControl == controls[kPOPUP_ABUS])
  753.                             editmir.bits.a = GetControlValue(theControl) - 1;
  754.                         else if (theControl == controls[kPOPUP_BBUS])
  755.                             editmir.bits.b = GetControlValue(theControl) - 1;
  756.                         else if (theControl == controls[kPOPUP_CBUS]) {
  757.                             if (editmir.bits.c != (newval = (GetControlValue(theControl)-2))) {
  758.                                 ChangeAnim_cbus(newval < 0, editmir.bits.dsc);
  759.                                 editmir.bits.c = newval;
  760.                                 editmir.bits.dsc = newval < 0;
  761.                                 }
  762.                             }
  763.                         else if (theControl == controls[kPOPUP_BUSREQ]) {
  764.                             register short newvalrd, newvalwr;
  765.         
  766.                             newvalrd = ((newval = GetControlValue(theControl)) == kREQ_READ);
  767.                             newvalwr = (newval == kREQ_WRITE);
  768.                             if ((editmir.bits.rd != newvalrd)||(editmir.bits.wr != newvalwr)) {
  769.                                 ChangeAnim_busreq(newval, 1 + editmir.bits.rd + (editmir.bits.wr << 1));
  770.                                 editmir.bits.rd = newvalrd;
  771.                                 editmir.bits.wr = newvalwr;
  772.                                 }
  773.                             }
  774.                         else if (theControl == controls[kPOPUP_SHIFTER]) {
  775.                             if (editmir.bits.shift != (newval = (GetControlValue(theControl)-1))) {
  776.                                 ChangeAnim_sh(newval, editmir.bits.shift);
  777.                                 editmir.bits.shift = newval;
  778.                                 }
  779.                             }
  780.                         else if (theControl == controls[kPOPUP_BRANCH]) {
  781.                             if (editmir.bits.cond != (newval = (GetControlValue(theControl)-1))) {
  782.                                 ChangeAnim_cond(newval, editmir.bits.cond);
  783.                                 editmir.bits.cond = newval;
  784.                                 }
  785.                             }
  786.                         else if (theControl == controls[kPOPUP_ALU]) {
  787.                             if (editmir.bits.alu != (newval = (GetControlValue(theControl)-1))) {
  788.                                 ChangeAnim_alu(newval, editmir.bits.alu);
  789.                                 editmir.bits.alu = newval;
  790.                                 }
  791.                             }
  792.                         break;
  793.                     case kControlCheckBoxPart:
  794.                         if (theControl == controls[kRADIO_ABUS] || theControl == controls[kRADIO_MBR]) {
  795.                             if (RadioSelected != theControl) {
  796.                                 SetControlValue(RadioSelected, 0);
  797.                                 SetControlValue(theControl, 1);
  798.                                 RadioSelected = theControl;
  799.                                 ChangeAnim_amux(editmir.bits.amux = GetControlValue(controls[kRADIO_MBR]));
  800.                                 }
  801.                             }
  802.                         else {    
  803.                             SetControlValue(theControl, newval = (1 - GetControlValue(theControl)));
  804.                             if (theControl == controls[kCHECK_MAR]) {
  805.                                 editmir.bits.mar = newval;
  806.                                 ChangeAnim_mar(newval);
  807.                                 }
  808.                             else if (theControl == controls[kCHECK_MBR]) {
  809.                                 editmir.bits.mbr = newval;
  810.                                 ChangeAnim_mbr(newval);
  811.                                 }
  812.                             else if (theControl == controls[kCHECK_ACTMAP]) {
  813.                                 editmir.bits.map = newval;
  814.                                 ChangeAnim_map(newval);
  815.                                 }
  816.                             }
  817.                     }
  818.                 if (editmir.cstore != oldmir.cstore) {
  819.                     UpdateMir(editmir);
  820.                     /* …and update the control store memory */
  821.                     *(gCsMemory + theSelection[kL_COMMENTS]) = editmir;
  822.                     DocumentIsDirty(true);
  823.                     }
  824.                 }
  825.             }
  826.         }
  827.     }
  828. }
  829.  
  830. void Key_Microprogram_Ed(EventRecord *theEvent, Boolean ignoreChar)
  831. {
  832. enum {
  833. kTabKey = 9
  834. };
  835.  
  836. GrafPtr    savePort;
  837. unsigned char    ch;
  838.  
  839. GetPort(&savePort);
  840. SetPort(gWPtr_Microprogram_Ed);
  841. if ((ch = CHARFROMMESSAGE(theEvent->message)) == kTabKey) {
  842.     if (keyDownDest >= kKEY_LIST)
  843.         UnFrame();
  844.     keyDownDest = (theEvent->modifiers & shiftKey ?
  845.                     (keyDownDest == kKEY_INSTR ? kKEY_BRTO : keyDownDest + 1) :
  846.                     (keyDownDest == kKEY_BRTO ? kKEY_INSTR : keyDownDest - 1));
  847.     if (keyDownDest < kKEY_LIST) {
  848.         SettheInputTo(TEs[keyDownDest]);
  849.         TESetSelect(0, SHRT_MAX, gTheInput);
  850.         }
  851.     else {
  852.         SettheInputTo(nil);
  853.         Frame();
  854.         }
  855.     }
  856. else if (gTheInput) {
  857.     if (ignoreChar == false)
  858.         TEKey(ch, gTheInput);
  859.     if ((ch < kcursLeft)||(ch > kcursDown))
  860.         Microprog_TextWasModified();
  861.     }
  862. else if (keyDownDest >= kKEY_LIST)
  863.     DoChar(keyDownDest - 2, ch);
  864. SetPort(savePort);
  865. }
  866.  
  867. static void DoChar(short item, unsigned char theChar)
  868. {
  869. short    newSelect;
  870.  
  871. /* Take the char, and find the first line that starts with this char. */
  872. if ((newSelect = StartsWith(item, theChar)) != theSelection[item])
  873.     /* We've moved. Unhighlight the old. */
  874.     SelectLLine(item, newSelect);
  875. }
  876.  
  877. static short StartsWith(short elem, unsigned char c)
  878. {
  879. /* Check for cursor up, down. */
  880. if (c == kcursDown) {
  881.     /* Go up one, clamp at the end. */
  882.     return ((theSelection[elem] == maxLLine[elem]) ? theSelection[elem] :
  883.                 theSelection[elem] + 1);
  884.     }
  885. if (c == kcursUp) {
  886.     /* Go down one, clamp at the bottom. */
  887.     return((theSelection[elem] == 0) ? 0 : theSelection[elem] - 1);
  888.     }
  889. return (maxLLine[elem]);
  890. }
  891.  
  892. /* ResetMirAndComment: reset the micro instruction register and comment field */
  893.  
  894. static void ResetMirAndComment(void)
  895. {
  896. short    null = 0;
  897.  
  898. TESetText(&null, 0, TEs[kKEY_COMMENT]);
  899. RefreshTE(kKEY_COMMENT);
  900. ChangedComment();
  901. SetMir(0L);
  902. DocumentIsDirty(true);
  903. }
  904.  
  905. /* SendClipMsg: handle a clipboard request from the user */
  906.  
  907. OSErr SendClipMsg(int msg)
  908. {
  909. unsigned long    dataOffset;
  910. long    length;
  911. Handle    typedText;
  912. SignedByte    savedState;
  913.  
  914. switch (msg) {
  915.     case kCLIPMSG_CUT:
  916.         if ((length = ZeroScrap()) == noErr)
  917.             if ((length = PutScrap(kLEN_MIR, kFCR_MINE, (Ptr)&editmir)) == noErr) {
  918.                 savedState = WantThisHandleSafe(typedText = (Handle)TEGetText(TEs[kKEY_COMMENT]));
  919.                 length = PutScrap(InlineGetHandleSize(typedText), 'TEXT', *typedText);
  920.                 HSetState(typedText, savedState);
  921.                 if (length == noErr) {
  922.                     (void)TEFromScrap();
  923.                     ResetMirAndComment();
  924.                     }
  925.                 }
  926.         break;
  927.     case kCLIPMSG_COPY:
  928.         if ((length = ZeroScrap()) == noErr)
  929.             if ((length = PutScrap(kLEN_MIR, kFCR_MINE, (Ptr)&editmir)) == noErr) {
  930.                 savedState = WantThisHandleSafe(typedText = (Handle)TEGetText(TEs[kKEY_COMMENT]));
  931.                 length = PutScrap(InlineGetHandleSize(typedText), 'TEXT', *typedText);
  932.                 HSetState(typedText, savedState);
  933.                 (void)TEFromScrap();
  934.                 }
  935.         break;
  936.     case kCLIPMSG_PASTE:
  937.         typedText = NewHandle(0);
  938.         if ((length = GetScrap(typedText, kFCR_MINE, (long *)&dataOffset)) == kLEN_MIR) {
  939.             SetMir(*(unsigned long *)*typedText);
  940.             DocumentIsDirty(true);
  941.             if ((length = GetScrap(typedText, 'TEXT', (long *)&dataOffset)) >= 0L) {
  942.                 savedState = WantThisHandleSafe(typedText);
  943.                 TESetText(*typedText, length, TEs[kKEY_COMMENT]);
  944.                 HSetState(typedText, savedState);
  945.                 RefreshTE(kKEY_COMMENT);
  946.                 ChangedComment();
  947.                 }
  948.             }
  949.         DisposeHandle(typedText);
  950.         break;
  951.     case kCLIPMSG_CLEAR:
  952.         ResetMirAndComment();
  953.         length = 0L;
  954.         break;
  955.     }
  956. return((length < 0 && length != noTypeErr) ? length : noErr);
  957. }
  958.  
  959. /* SelectLLine: selects a specified line in a list */
  960.  
  961. void SelectLLine(short whichList, short newSelect)
  962. {
  963. Point    cell;
  964.  
  965. cell.h = 0;
  966. cell.v = theSelection[whichList];
  967. LSetSelect(false, cell, Lists[whichList]);
  968. cell.v = newSelect;
  969. LSetSelect(true, cell, Lists[whichList]);
  970. LAutoScroll(Lists[whichList]);
  971. ChangedListSelection(cell, whichList, false);
  972. }
  973.  
  974. /* SetMir: calls UpdateMir and updates the document (Microprogram Editor) window */
  975.  
  976. void SetMir(unsigned long newmir)
  977. {
  978. union u_mir    oldmir;
  979.  
  980. oldmir = editmir;
  981. editmir.cstore = newmir;
  982. UpdateMir(editmir);
  983. /* …and update the control store memory */
  984. *(gCsMemory + theSelection[kL_COMMENTS]) = editmir;
  985. SetControlsFromMir(oldmir);
  986. }
  987.  
  988.  
  989. /* procedure called when closing the MPrg window */
  990.  
  991. void CloseMicroProg(WindowPtr w)
  992. {
  993. DoCloseWindow(w, kMItem_Microprogram);
  994. }
  995.  
  996. void GotDragOnComments(void)
  997. {
  998. Rect    tempRect;
  999. Point    curMouse;
  1000. ListHandle    theList;
  1001. ListClickLoopUPP    savedLCkLoopUPP;
  1002. Boolean    wasLinked;
  1003.  
  1004. tempRect = *keyrects[kKEY_LIST];
  1005. tempRect.right -= kScrollbarAdjust;
  1006. SetCursor(*GetCursor(kSheetsCursor));
  1007. GetMouse(&curMouse);
  1008. theList = Lists[kL_COMMENTS];
  1009. savedLCkLoopUPP = (*theList)->lClickLoop;
  1010. (*theList)->lClickLoop = gSwitchCursClikLoopUPP;
  1011.  
  1012. (void)LClick(curMouse, gMyEvent.modifiers, theList);
  1013. (*theList)->lClickLoop = savedLCkLoopUPP;
  1014. GetMouse(&curMouse);
  1015. wasLinked = PtInRect(curMouse, &tempRect);
  1016. *(long *)&curMouse = 0L;
  1017. if (LGetSelect(true, &curMouse, theList)) {
  1018.     if (wasLinked) {
  1019.         SetCursor(*GetCursor(kLinkedSheetsCursor));
  1020.         DoSound(kSndPinzatrice);
  1021.         DocumentIsDirty(true);
  1022.         ChangedListSelection(curMouse, kL_COMMENTS, true);
  1023.         }
  1024.     else {
  1025.         LSetSelect(false, curMouse, theList);
  1026.         curMouse.h = 0;
  1027.         curMouse.v = theSelection[kL_COMMENTS];
  1028.         LSetSelect(true, curMouse, theList);
  1029.         SysBeep(30);
  1030.         }
  1031.     }
  1032. InitCursor();
  1033. }
  1034.  
  1035. void Update_Microprogram_Ed(WindowPtr w, short)
  1036. {
  1037. PenState    curPen;
  1038. Rect    tempRect;
  1039. Rect    tempRectInstr;
  1040. WindowPtr    f = FrontWindow();
  1041. Handle    tempH;
  1042.  
  1043. //PaintRgn(w->visRgn);
  1044. TextFont(systemFont);
  1045. TextSize(12);
  1046. HLock(tempH = Get1Resource('STR#', kW_STRINGS));
  1047. MoveTo(keyrects[kKEY_STRINGS]->left, keyrects[kKEY_STRINGS]->top);
  1048. DrawString((ConstStr255Param)GetPtrIndHString(tempH, kSTR_ALUINPUT - 1));
  1049. MoveTo(keyrects[kKEY_STRINGS]->right, keyrects[kKEY_STRINGS]->bottom);
  1050. DrawString((ConstStr255Param)GetPtrIndHString(tempH, kSTR_TO - 1));
  1051. HUnlock(tempH);
  1052. FrameRect(keyrects[kKEY_COMMENT]);
  1053. FrameRect(keyrects[kKEY_BRTO]);
  1054. tempRect = *keyrects[kKEY_LIST];
  1055. tempRect.right -= kScrollbarAdjust;
  1056. InsetRect(&tempRect, -1, -1);
  1057. FrameRect(&tempRect);
  1058. tempRectInstr = *keyrects[kKEY_INSTR];
  1059. tempRectInstr.right -= kScrollbarAdjust;
  1060. InsetRect(&tempRectInstr, -1, -1);
  1061. FrameRect(&tempRectInstr);
  1062.  
  1063. GetPenState(&curPen);
  1064. PenNormal();
  1065. PenSize(2, 2);
  1066. if (keyDownDest != kKEY_INSTR || w != f)
  1067.     PenMode(patBic);
  1068. tempRectInstr.right += kScrollbarAdjust;
  1069. InsetRect(&tempRectInstr, -3, -3);
  1070. FrameRect(&tempRectInstr);
  1071. PenMode(keyDownDest == kKEY_LIST && w == f ? patCopy : patBic);
  1072. tempRect.right += kScrollbarAdjust;
  1073. InsetRect(&tempRect, -3, -3);
  1074. FrameRect(&tempRect);
  1075. SetPenState(&curPen);
  1076.  
  1077. TextFont(geneva);
  1078. TextSize(9);
  1079. LUpdate(w->visRgn, Lists[kL_INSTR]);
  1080. LUpdate(w->visRgn, Lists[kL_COMMENTS]);
  1081. RefreshTE(kKEY_BRTO);
  1082. RefreshTE(kKEY_COMMENT);
  1083. UpdateControls(w, w->visRgn);
  1084. }
  1085.  
  1086.  
  1087. void SetMicroProgramComment(const Ptr text, UInt32 len)
  1088. {
  1089. TESetText(text, len, TEs[kKEY_COMMENT]);
  1090. }
  1091.  
  1092. Boolean IsTEBranchTo(TEHandle te)
  1093. {
  1094. return te == TEs[kKEY_BRTO];
  1095. }
  1096.  
  1097. Boolean IsTEComment(TEHandle te)
  1098. {
  1099. return te == TEs[kKEY_COMMENT];
  1100. }
  1101.  
  1102.  
  1103. #endif
  1104.  
  1105.